<script>
	import config from './config'
	// import store from '@/store'
	import {
		getToken
	} from '@/utils/auth'

	export default {
		onLaunch: async function(options) {
			let that = this;
			// #ifdef APP-PLUS
			plus.navigator.closeSplashscreen()
			that.checkAppUpdate().finally(() => {
				uni.hideLoading();
			})
			// #endif
		},

		data() {
			return {
				newArr: {}
			}
		},
		methods: {
			//检查app更新
			checkAppUpdate() {
				console.log('kadljadlkasdlj')
				let that = this;
				return new Promise((resolve, reject) => {
					plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
						console.log('111111111',widgetInfo.version,'222')
						uni.request({
							url: 'https://wxb.xiangwangsoft.top:666/clap/update/checkUpdate',
							data: {
								version: widgetInfo.version,
								name: widgetInfo.name
							},  
							success: (result) => {
								console.log(result)
								that.newArr = result.data;
								that.checkOK()
							}
						})
					})
				})
			},
			//确认更新
			checkOK() {
				let that = this;
				console.log(that.newArr.wgtUrl)
				if (that.newArr.update && that.newArr.wgtUrl) {
					console.toLocaleString(that.newArr.update)
					// 需要更新
					this.needUpdate = true;
					uni.showModal({
						title: '版本更新提示',
						content: 'APP发现新版本，请进行更新',
						confirmText: '更新',
						confirmColor: '#cb0725',  
						success: res => {
							console.log(res.confirm)
							if (res.confirm === true) {
								that.checkWgtUpdate();
							} else {
								uni.hideLoading()
								setTimeout(() => {
								}, 3000)
							}
						}
					})
				}
			},
			//更新
			checkWgtUpdate() {
				let that = this;
				plus.nativeUI.showWaiting("更新中...");
				console.log(that.newArr.wgtUrl)
				let downloadTask = uni.downloadFile({ //执行下载
					url: that.newArr.wgtUrl,
					success: downloadResult => {
						console.log('下载', downloadResult)
						uni.hideLoading()
						if (downloadResult.statusCode === 200) {
							plus.runtime.install(downloadResult.tempFilePath, {
									force: true
								},
								function() {
									console.log("更新成功")
									plus.nativeUI.toast("更新成功");
									plus.runtime.restart();
								},
								function(e) {
									console.log("更新失败")
									plus.nativeUI.closeWaiting();
									utils.showToast('更新失败');
								}
							)
						}  
						plus.nativeUI.closeWaiting();
					},
					fail: error => {
						console.log(error)
					}
				})
				//控制台打印下载包进度
				downloadTask.onProgressUpdate(res => {
					console.log(res.progress)
				})
				// downloadTask.start();
			},
			// 初始化应用
			initApp() {
				// 初始化应用配置
				this.initConfig()
				// 检查用户登录状态
				//#ifdef H5
				// this.checkLogin()
				//#endif
			},
			initConfig() {
				this.globalData.config = config
				this.globalData.firstCode = 927360369
				this.globalData.secondCode = 213261619
			},
			checkLogin() {
				if (!getToken()) {
					this.$tab.reLaunch('/pages/login')
				}
			}
		}
	}
</script>

<style lang="scss">
	@import '@/static/scss/index.scss';
	@import '@/static/scss/common.css';
	@import "@/uni_modules/uview-ui/index.scss";
</style>